﻿using OddUserManager.Data.IRepositories;
using OddUserManager.Model;
using System;
using System.Collections.Generic;
using System.Text;
using Dapper;
using System.Linq;
namespace OddUserManager.Data.Repositories
{
    public class SystemConstantRepository : DapperBaseRepository<SystemConstant, int>, ISystemConstantRepository
    {
        public bool InsertOrUpdate(SystemConstant entity)
        {
            if (Get(entity.Id) == null)
            {
                using (var con = GetOpenConnection())
                {
                    var sql = $"selct count(*)  from SystemConstant where ConstantKey={entity.ConstantKey}";
                    var result = con.Query<int>(sql).FirstOrDefault();
                    if (result > 0)
                    {
                        throw new Exception("数据库存在相同名字的键");
                    }
                }

                Insert(entity);
            }
            else
            {
                Update(entity);
            }
            return true;
        }

        public IEnumerable<SystemConstant> LoadPageList(int startPage, int pageSize, out int rowCount, Func<SystemConstant, bool> where, Func<SystemConstant, object> order)
        {
            throw new NotImplementedException();
        }
    }
}
